package io.basic.byte_;

import org.junit.Test;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileCopy {
    @Test
    public void filCopy1() {
        FileInputStream in = null;
        FileOutputStream out = null;
        String filePath = "/Users/tomdayu/home/hello.txt";
        String filePath1 = "/Users/tomdayu/home/hello2.txt";

        try {
            in = new FileInputStream(filePath);
            out = new FileOutputStream(filePath1);

            byte[] buf = new byte[1024];
            int readLen = 0;
            //边输入边输出
            while ((readLen = in.read(buf)) != -1) {
//                out.write(buf);//writeBytes(b, 0, b.length, append); 拷到最后会出问题，因为可能没有b.length之多
                out.write(buf, 0, readLen);//有多少读多少 拷贝图片一定要用这个方法
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
